<?php
/**
 *
 * @package    mahara
 * @subpackage artefact-feedback
 * @author     laurent.opprecht@gmail.com
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2011 University of Geneva http://www.unige.ch/
 *
 */

/**
 * A form to add feedback to a "parent" i.e. a block.
 */
class add_parentfeedback_form {

    /**
     * Create and return the structure of the form.
     *
     * @global  $USER
     * @return array|pieform
     */
    public static function create($parent_id=0, $goto='', $defaultprivate = false) {
        global $USER;

        if (!$USER->is_logged_in()) {
            return array();
        }

        $form = array(
            'name' => 'add_parentfeedback_form',
            'method' => 'post',
            'plugintype' => 'artefact',
            'pluginname' => 'feedback',
            'jsform' => true,
            'autofocus' => false,
            'elements' => array(),
            'jssuccesscallback' => 'addParentFeedbackSuccess',
        );

        $form['elements']['to'] = array(
            'type' => 'hidden',
            'value' => $parent_id,
        );
        $form['elements']['goto'] = array(
            'type' => 'hidden',
            'value' => $goto,
        );
        $form['elements']['message'] = array(
            'type' => 'wysiwyg',
            'title' => get_string('message'),
            'rows' => 5,
            'cols' => 80,
        );
        $form['elements']['ispublic'] = array(
            'type' => 'checkbox',
            'title' => get_string('public', 'artefact.feedback'),
            'defaultvalue' => !$defaultprivate,
        );
        $form['elements']['submit'] = array(
            'type' => 'submitcancel',
            'value' => array(get_string('submit'), get_string('cancel')),
        );
        $result = pieform($form);
        return $result;
    }

    /**
     * Reset the form. Remove posted values.
     *
     * @param Pieform $form
     */
    public function reset(Pieform $form) {
        $_POST[$form->get_element('message')] = null;
        $_POST[$form->get_element('ispublic')] = null;
    }

    /**
     * Validate the form before processing it.
     *
     * @param Pieform $form
     * @param $values
     */
    public static function validate(Pieform $form, $values) {
        if ($form->get_property('spam')) {
            require_once(get_config('libroot') . 'antispam.php');
            $spamtrap = new_spam_trap(array(
                        array(
                            'type' => 'body',
                            'value' => $values['message'],
                        ),
                    ));

            if ($form->spam_error() || $spamtrap->is_spam()) {
                $msg = get_string('formerror');
                $emailcontact = get_config('emailcontact');
                if (!empty($emailcontact)) {
                    $msg .= ' ' . get_string('formerroremail', 'mahara', $emailcontact, $emailcontact);
                }
                $form->set_error('message', $msg);
            }
        }
        if (empty($values['attachments']) && empty($values['message'])) {
            $form->set_error('message', get_string('messageempty', 'artefact.feedback'));
        }
    }

    /**
     * Process the form if it is valid and the user clicked on the submit button.
     *
     * @global View $view
     * @global <type> $USER
     * @param Pieform $form
     * @param <type> $values
     */
    public static function submit(Pieform $form, $values) {
        global $view, $USER;

        $values['title'] = get_string('Feedback', 'artefact.feedback');
        $values['description'] = $values['message'];
        $values['private'] = ! $values['ispublic'];
        $feedback = new ArtefactTypeFeedback(0, $values);

        $feedback->commit();

        $goto = $form->get_element('goto');
        $goto = $form->get_value($goto);
        $parent_id = $values['to'];
        $view_id = param_integer('view_id');
        $show_private =  ArtefactTypeFeedback::is_view_owner($view_id);
        $limit = 10;
        $offset = 0;

        reset($form);

        $count = ArtefactTypeFeedback::count_feedbacks_onparent($parent_id, $show_private);
        $rows = FeedbackTable::build_rows($parent_id, $show_private, $limit, $offset);
        $pagination = FeedbackTable::build_pagination($parent_id, $show_private, $count, $limit, $offset);

        $data = array(
            'count' => $count,
            'limit' => $limit,
            'offset' => $offset,
            'tablerows' => $rows,
            'pagination' => $pagination['html'],
            'pagination_js' => $pagination['javascript'],
        );


        $form->json_reply(PIEFORM_OK, array(
            'message' => get_string('feedbacksubmitted', 'artefact.feedback'),
            'goto' => $goto,
            'parent_id' => $values['to'],
            'data' => $data,
        ));
    }

    /**
     * If the user canceled the form, redirect him to the current view.
     *
     * @global View $view
     * @param Pieform $form
     */
    function cancel(Pieform $form) {
        $goto = $form->get_element('goto');
        $goto = $form->get_value($goto);
        reset($form);
        $form->json_reply(PIEFORM_OK, array(
            'goto' => $goto
        ));
    }

}

/**
 * Stubs to interact with Pie form architecture.
 */

/**
 * Validate the form.
 *
 * @param Pieform $form
 * @param array $values
 */
function add_parentfeedback_form_validate(Pieform $form, $values) {
    return add_parentfeedback_form::validate($form, $values);
}

/**
 * Process the form if it is valid.
 *
 * @param Pieform $form
 * @param array $values
 */
function add_parentfeedback_form_submit(Pieform $form, $values) {
    return add_parentfeedback_form::submit($form, $values);
}

/**
 * If the user canceled the form, redirect him to the current view.
 * 
 * @param Pieform $form
 */
function add_parentfeedback_form_cancel_submit(Pieform $form) {
    return add_parentfeedback_form::cancel($form, $values);
}

?>
